﻿using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;

namespace ldhWebApiWF
{
    /// <summary>
    /// Echoes messages sent using the Send message by calling the
    /// addMessage method on the client. Also reports to the console
    /// when clients connect and disconnect.
    /// </summary>
    public class MyHub : Hub
    {
        public void Send(string name, string message)
        {
            Clients.All.addMessage(name, message);
        }
        public override Task OnConnected()
        {
            //Use Application.Current.Dispatcher to access UI thread from outside the MainWindow class
            //Application.Current.Dispatcher.Invoke(() =>
            //    ((MainWindow)Application.Current.MainWindow).WriteToConsole("Client connected: " + Context.ConnectionId));
            //Console.WriteLine("Client connected: " + Context.ConnectionId);
            Program.form.lb_msg.Items.Insert(0, "Client connected: " + Context.ConnectionId);
            return base.OnConnected();
        }

        public override Task OnDisconnected(bool stopCalled)
        {
            //Use Application.Current.Dispatcher to access UI thread from outside the MainWindow class
            //Application.Current.Dispatcher.Invoke(() =>
            //    ((MainWindow)Application.Current.MainWindow).WriteToConsole("Client disconnected: " + Context.ConnectionId));
            //Console.WriteLine("Client disconnected: " + Context.ConnectionId);
            Program.form.lb_msg.Items.Insert(0, "Client disconnected: " + Context.ConnectionId);
            return base.OnDisconnected(stopCalled);
        }

    }
}
